﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.Business;
using OpenGado.DATA.Entities;
using OpenGado.DATA;

namespace OpenGado.Win
{
    public partial class frmProductSearch : Form
    {

        ProductBusiness pb = new ProductBusiness();



        public frmProductSearch()
        {
            InitializeComponent();
        }


        private void GetProductData()
        {
                    dgvProductList.AutoGenerateColumns = false;

                    dgvProductList.Rows.Clear();

                     IList<Product> productList = new List<Product>();

                    // check  whatis being searched for 
            if (!String.IsNullOrEmpty(txtSearchText.Text))
	            {
		
                    switch (cboSearchBy.Text)
                    {
                        case "SKU":
                            {
                                productList = pb.GetProductsByCriteria("SKU", txtSearchText.Text);
                            }

                            break;

                        case "Code":
                            {
                                productList = pb.GetProductsByCriteria("BatchNumber", txtSearchText.Text);
                            }

                            break;

                        case "Name":
                            {
                                productList = pb.GetProductsByCriteria("ProductName", txtSearchText.Text);
                            }

                            break;
                        case "Location":
                            {
                                productList = pb.GetProductsByCriteria("Storage", txtSearchText.Text);
                            }

                            break;
                        case "Project":
                            {
                                productList = pb.GetProductsByCriteria("Project", txtSearchText.Text);
                            }

                            break;
                    } 
	        }
            else
            {
                productList = pb.GetAllProducts();
            }
           

                    foreach (Product p in productList)
                    {
                        DataGridViewRow rw = new DataGridViewRow();
                        int n = dgvProductList.Rows.Add(rw);

                        rw.Tag = p;
                        dgvProductList.Rows[n].Cells[0].Value = p.Productid;
                        dgvProductList.Rows[n].Cells[1].Value = p.ProductName;
                        dgvProductList.Rows[n].Cells[2].Value = p.BatchNumber;
                        dgvProductList.Rows[n].Cells[3].Value = p.ProductDescription;
                        dgvProductList.Rows[n].Cells[4].Value = p.Sku;
                        dgvProductList.Rows[n].Cells[5].Value = p.ProductQuantity.ToString();
                        dgvProductList.Rows[n].Cells[6].Value = p.UnitOfMeasure.Name;
                        dgvProductList.Rows[n].Cells[7].ValueType = typeof(DateTime);
                        dgvProductList.Rows[n].Cells[7].Value = p.ProductExpiry;
                        dgvProductList.Rows[n].Cells[8].Value = p.Storage.StoreName;
                        dgvProductList.Rows[n].Cells[9].Value = p.Project.Name;
               
                    }

           
                }

        private void ProductSearch_Load(object sender, EventArgs e)
        {
            GetProductData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmProduct productForm = new frmProduct();
            productForm.UpdateProductList += new frmProduct.UpdateProductListHandler(GetProductData);
            productForm.Show();

        }

        private void dgvProductList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //get current product id 
            int productid;

            int.TryParse(dgvProductList.CurrentRow.Cells[0].Value.ToString(), out productid);

            frmProduct productForm = new frmProduct(productid);
            productForm.UpdateProductList += new frmProduct.UpdateProductListHandler(GetProductData);
            productForm.Show();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            DialogResult res;
            res = MessageBox.Show("Do you really want  to delete this selection ? ", "Inventory", MessageBoxButtons.YesNo);
            if (res == DialogResult.Yes)
            {
                int productid;

                foreach (int i in SelectedProducts())
                {
                    Product p = new Product();
                    p = ProductDataControl.Instance.GetById(i);
                    ProductDataControl.Instance.Delete(p);
                }
               
                

                GetProductData();
            }
           

            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            GetProductData(); 
        }

        private List<int> SelectedProducts()
        {
            List<int> selectedProductsList = new List<int>();

            int productId;
            foreach (DataGridViewRow rw in dgvProductList.SelectedRows)
            {
                int.TryParse(rw.Cells[0].Value.ToString(), out productId);

                // if product doesnt exist in the list then add it.
               if (!selectedProductsList.Contains(productId))
               {
                   selectedProductsList.Add(productId);
               }
               
            }

            return selectedProductsList;

        }

        private void btnIssue_Click(object sender, EventArgs e)
        {
           // Add the items to the issue list
        }

        private void ShowProductCountMessage()
        {
            int i = 0;
            i = dgvProductList.Rows.Count;

            String message;
            message = "Showing " + i.ToString() + " item(s)";
            lblShowing.Text = message;
        }

        private void dgvProductList_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            ShowProductCountMessage();
        }

        private void dgvProductList_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            ShowProductCountMessage();
        }
    }

  
}
